<template>
  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <!-- <el-table-column label="编号" align="center" prop="id" /> -->
      <el-table-column label="参与人" align="center" prop="userName" />
      <!-- <el-table-column label="部门ID" align="center" prop="deptId" /> -->
      
      <el-table-column label="活动参与信息" align="center">
        <el-table-column label="部门名称" align="center" prop="deptName" />
        <el-table-column label="参与文件" align="center" prop="fileUrls" :show-overflow-tooltip="false">
          <template #default="scope">
            <FileListView :file-list="scope.row.fileUrls" />
          </template>
        </el-table-column>
        <el-table-column
          label="参与时间"
          align="center"
          prop="createTime"
          :formatter="dateFormatter"
          width="180px"
        />
      </el-table-column>

      <el-table-column label="活动批改信息" align="center">
        <el-table-column label="评价" align="center" prop="feedback" width="300"/>
        <el-table-column label="评分" align="center" prop="score" />
        <el-table-column label="批改文件" align="center" prop="feedbackUrls">
          <template #default="scope">
            <FileListView :file-list="scope.row.feedbackUrls" />
          </template>
        </el-table-column>

        <el-table-column label="批改人" align="center" prop="updaterName" />
        <el-table-column
          label="批改时间"
          align="center"
          prop="updateTime"
          :formatter="dateFormatter"
          width="180px"
        />
      </el-table-column>

      <el-table-column label="操作" align="center" fixed="right" width="120">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['school:activity:update']"
            :disabled="currentUserId == scope.row.creator"
          >
            批改
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['school:activity:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
  <!-- 表单弹窗：添加/修改 -->
  <ActivityRecordForm ref="formRef" @success="getList" />
</template>
<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import { ActivityApi } from '@/api/school/activity'
import ActivityRecordForm from './ActivityRecordForm.vue'
import { CACHE_KEY, useCache } from '@/hooks/web/useCache'

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗
const route = useRoute() // 路由
const { wsCache } = useCache()
const currentUser = wsCache.get(CACHE_KEY.USER).user
const currentUserId = currentUser?.id // 当前用户ID

const loading = ref(false) // 列表的加载中
const list = ref([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  activityId: route.params.activityId,
  userId: route.params.userId
})

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await ActivityApi.getActivityRecordPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await ActivityApi.deleteActivityRecord(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
